#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sysexits.h>
#include "hashtable.h"


#define MAX_LEN 1024

int main(int argc, char *argv[]) {

   FILE *input;
   char buffer[MAX_LEN + 1];
   char filename[MAX_LEN + 1];
   char command[MAX_LEN + 1];
   Table t;
   char key[MAX_LEN + 1];
   char value[MAX_LEN + 1];

   init_table(&t);

   printf("Input file name (e.g., data.txt): ");
   scanf("%s", filename);

   if (argc == 1) {
      input = stdin;

   }

   if (argc == 2) {
      input = fopen(argv[1], "r");
      if (input == NULL) {
         fprintf(stderr, "Error!\n");
         exit(EX_OSERR);
      }

   }

   if (argc > 2) {
      fprintf(stderr, "Error!\n");
      exit(EX_USAGE);
   }

   while (fgets(buffer, MAX_LEN + 1, input) != NULL) {

     
      if (buffer[0] == '#' || strlen(buffer) == 0) {
         /*do nothing */

      } else {

         sscanf(buffer, " %s", command);

         if (strcmp(command, "insert") == 0) {

            sscanf(buffer, "%s %s %s", command, key, value);
            int result = insert(&t, key, value);
            if (strcmp(key, "") == 0 && strcmp(value, "") == 0) {
               fprintf(stderr, "Error! Input is wrong");
               exit(EX_DATAERR);

            }
            if (result == 0) {
               printf("Insertion of %s => %s succeeded. \n", key, value);

            } else {
               printf("Insertion of %s => %s failed. \n", key, value);

            }

         }

         if (strcmp(command, "search") == 0) {

            sscanf(buffer, "%s %s %s", command, key, value);
            if (strcmp(key, "") == 0 && strcmp(value, "") == 0) {
               fprintf(stderr, "Error! Input is wrong");
               exit(EX_DATAERR);

               int result = search(&t, key, value);
               if (result == 0) {
                  printf("Search for %s succeeded.", key);

               } else {
                  printf("Search for %s failed.", key);

               }
            }

            if (strcmp(command, "delete") == 0) {

               sscanf(buffer, " %s %s", command, key);
               if (strcmp(key, "") == 0 && strcmp(value, "") == 0) {
                  fprintf(stderr, "Error! Input is wrong");
                  exit(EX_DATAERR);

                  int result = delete(&t, key);
                  if (result == 0) {
                     printf("Deletion of %s succeeded.", key);

                  } else {
                     printf("Deletion of %s failed.", key);
                  }
               }

               if (strcmp(command, "reset") == 0) {
                  reset_table(&t);
                  printf("Table reset");
               }

               if (strcmp(command, "display") == 0) {
                  sscanf(buffer, "%s %s", command, key);

                  if (strcmp(key, "key_count") == 0) {
                     printf("Key count: %d", key_count(&t));

                  }
                  if (strcmp(key, "table") == 0) {
                     int i;
                     while (i != NUM_BUCKETS) {
                        for (i = 0; i < NUM_BUCKETS - 1; i++) {
                           if (t.buckets[i].state == FULL) {
			     printf("Bucket %d : \n (%s => %s)",i,
                                     t.buckets[i].data.key,
                                     t.buckets[i].data.value);

                           } else {
                              printf("Bucket %d:\n", t.buckets[i].state);
                           }
                        }
                     }

                  }

               }
            }
         }
      }
      else{
	fprintf(stderr, "Error! Incorrect command");
	exit(EX_USAGE);
}
   }
}
